package com.winderinfo.yikaotianxia.ui.videolearning;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;


import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaseFragment;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.core.utils.SPUtils;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

public class CatalogFragment extends BaseFragment implements Contract.View {
    @BindView(R.id.b_recycle)
    RecyclerView bRecycle;

    private Presenter presenter;
    Map<String, Object> map = new HashMap<>();
    private SPUtils spUtils;
    private int id;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_banxin;
    }

    @Override
    protected void initView(View view) {
        id = getArguments().getInt("id");
        //实例P
        presenter = new Presenter(this);
        spUtils = new SPUtils(getActivity());
        bRecycle.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    @Override
    protected void initData() {
        //获取用户Id
        String userID = spUtils.getString("UserID", "");
        //请求课程列表
        /**
         * @goodsId 课程id
         * @userId 用户id
         * */
        map.clear();
        map.put("goodsId", id);
        map.put("userId", 1);
        presenter.onPostRequest(Api.KECHENG, map, LearningBean.class);
    }


    @Override
    public void onSuccess(Object data) {
        //课程数据
        if (data instanceof LearningBean) {
            List<LearningBean.RowsBean> rows = ((LearningBean) data).getRows();
            LearningAdapter learningAdapter = new LearningAdapter(getActivity(), rows);
            bRecycle.setAdapter(learningAdapter);
            learningAdapter.setOnUrl(new LearningAdapter.OnUrl() {
                @Override
                public void getId(String url) {
                    if (onCataUrl != null) {
                        onCataUrl.getId(url);
                    }
                }
            });
        }
    }

    @Override
    public void onFailure(Object error) {

    }

    //接口回调
    public interface OnCataUrl {
        void getId(String url);
    }

    public OnCataUrl onCataUrl;

    public void setOnCataUrl(OnCataUrl onCataUrl) {
        this.onCataUrl = onCataUrl;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.onUntying();
    }
}
